package com.xueyi.aixihu.mapper;

import com.xueyi.aixihu.entity.po.AxhFinanceLog;
import com.xueyi.aixihu.reqeust.AuditFinanceReq;
import com.xueyi.aixihu.reqeust.FindAuditFinanceDto;
import com.xueyi.aixihu.reqeust.OpenNewsReq;
import com.xueyi.aixihu.response.FindAuditFinanceVo;
import org.apache.ibatis.annotations.Insert;
import org.apache.ibatis.annotations.Param;
import org.apache.ibatis.annotations.Select;
import org.apache.ibatis.annotations.Update;

import java.util.List;

/**
 * @author lvyi
 * @date : 2023/6/29
 * @Description :
 */
public interface FinanceAuditMapper {
    /**
     * 是否公开镇级财务公开
     *
     * @param openNewsReq
     */
    @Update(" UPDATE axh_finance SET is_public = #{req.isPublic} WHERE id = #{req.id} ")
    void openFinanceByZJCW(@Param("req") OpenNewsReq openNewsReq);

    /**
     * 同步更改审核记录的是否公开镇级财务公开
     *
     * @param openNewsReq
     * @param cwAudit
     */
    @Update(" UPDATE cdz_audit_record SET is_public = #{req.isPublic},last_command_time = NOW() " +
            " WHERE join_id = #{req.id} AND audit_type = #{cwAudit} ")
    void openFinanceByZJCWRecord(@Param("req") OpenNewsReq openNewsReq, @Param("cwAudit") Integer cwAudit);

    /**
     * 审核通过镇级财务公开
     *
     * @param auditFinanceReq
     */
    @Update(" UPDATE axh_finance SET current_status = #{req.currentStatus} WHERE id = #{req.id} ")
    void auditSuccessFinanceByZJCW(@Param("req") AuditFinanceReq auditFinanceReq);

    /**
     * 审核通过镇级党务公开的日志
     *
     * @param auditFinanceReq
     * @param cwAudit
     * @param id
     */
    @Update(" UPDATE cdz_audit_record SET current_status = #{req.currentStatus},last_command_time = NOW() " +
            " WHERE join_id = #{req.id} AND audit_type = #{cwAudit} ")
    void auditSuccessFinanceByZJCWRecord(@Param("req") AuditFinanceReq auditFinanceReq,
                                         @Param("cwAudit") Integer cwAudit,
                                         @Param("id") Integer id);

    /**
     * 审核不通过镇级财务公开
     *
     * @param auditFinanceReq
     */
    @Update(" UPDATE axh_finance SET current_status = #{req.currentStatus} WHERE id = #{req.id} ")
    void auditFailFinanceByZJCW(@Param("req") AuditFinanceReq auditFinanceReq);

    /**
     * 审核不通过镇级党务公开的日志
     *
     * @param auditFinanceReq
     * @param cwAudit
     * @param id
     */
    @Update(" UPDATE cdz_audit_record SET " +
            " current_status = #{expectFinanceStatus},last_command_time = NOW(),fail_msg = #{req.failMsg} " +
            " WHERE join_id = #{req.id} AND audit_type = #{cwAudit} ")
    void auditFailFinanceRecord(@Param("req") AuditFinanceReq auditFinanceReq, @Param("cwAudit") Integer cwAudit, @Param("id") Integer id,
                                @Param("expectFinanceStatus") Integer expectFinanceStatus);

    /**
     * 修改日志的审核状态
     *
     * @param axhFinanceLog
     */
    @Update(" UPDATE axh_finance_log SET " +
            " remark = #{req.remark},log_content = #{req.logContent},current_finance_status = #{req.currentFinanceStatus} " +
            " WHERE finance_id = #{req.financeId} ")
    void updateAuditLog(@Param("req") AxhFinanceLog axhFinanceLog);

    /**
     * 添加审核日志
     *
     * @param axhFinanceLog
     */
    @Insert(" INSERT INTO axh_finance_log (remark,log_content,current_finance_status,command_user_id,finance_id , log_type) " +
            " VALUES (#{req.remark},#{req.logContent},#{req.currentFinanceStatus},#{req.commandUserId},#{req.financeId} , 1) ")
    void addAuditLog(@Param("req") AxhFinanceLog axhFinanceLog);

    /**
     * 同意或者拒绝报表
     *
     * @param id
     * @param failMsg
     * @param expectFinanceStatus
     */
    @Update("UPDATE axh_finance SET current_status = #{expectFinanceStatus} , current_fail_msg = #{failMsg} WHERE id = #{id} ")
    void consentOrFailFinance(@Param("id") Integer id, @Param("failMsg") String failMsg, @Param("expectFinanceStatus") Integer expectFinanceStatus);

    /**
     * 查询符合条件的报表总条数
     *
     * @param findAuditFinanceDto
     * @param userManageAreaId
     * @param userAuditFinanceStatusStr
     * @param lastMonthDay
     * @return
     */
    @Select("<script>" +
            "SELECT COUNT(id) FROM axh_finance WHERE is_valid = 1 AND current_status IN (${userAuditFinanceStatusStr}) " +
            " <if test='dto.reportType != null'>" +
            "     AND report_type = #{dto.reportType}  " +
            "</if> " +
            " <if test='userManageAreaId != null'>" +
            "     AND from_to_area_id IN (${userManageAreaId})  " +
            "</if> " +
            " <if test='dto.keyword != null'>" +
            "     AND finance_title LIKE '%${dto.keyword}%'  " +
            "</if> " +
            " <if test='dto.month != null'>" +
            "     AND create_time &gt;= '${dto.month}-01 00:00:00' AND create_time &lt;= '${lastMonthDay}' " +
            "</if> " +
            " <if test='dto.fromToAreaIds != null'>" +
            "     AND from_to_area_id IN (${dto.fromToAreaIds})  " +
            "</if> " +
            " <if test='dto.areaTypeStr != null'>" +
            "     AND area_type IN (${dto.areaTypeStr})  " +
            "</if> " +
            "</script>")
    Integer findFinanceAuditCount(@Param("dto") FindAuditFinanceDto findAuditFinanceDto,
                                  @Param("userManageAreaId") String userManageAreaId,
                                  @Param("userAuditFinanceStatusStr") String userAuditFinanceStatusStr,
                                  @Param("lastMonthDay") String lastMonthDay);

    @Select("<script>" +
            " SELECT COUNT( DISTINCT a1.id ) FROM ( SELECT * FROM axh_finance WHERE is_valid = 1  " +
            " <if test='dto.reportType != null'>" +
            "     AND report_type = #{dto.reportType}  " +
            "</if> " +
            " <if test='userManageAreaId != null'>" +
            "     AND from_to_area_id IN (${userManageAreaId})  " +
            "</if> " +
            " <if test='dto.keyword != null'>" +
            "     AND finance_title LIKE '%${dto.keyword}%'  " +
            "</if> " +
            " <if test='dto.month != null'>" +
            "     AND create_time &gt;= '${dto.month}-01 00:00:00' AND create_time &lt;= '${lastMonthDay}' " +
            "</if> " +
            " <if test='dto.fromToAreaIds != null'>" +
            "     AND from_to_area_id IN (${dto.fromToAreaIds})  " +
            "</if> " +
            " <if test='dto.areaTypeStr != null'>" +
            "     AND area_type IN (${dto.areaTypeStr})  " +
            "</if> " +
            " ) a1 LEFT JOIN axh_finance_log afl ON a1.id = afl.finance_id " +
            " WHERE afl.command_user_id = #{userId} " +
            "</script>")
    Integer findFinanceAuditCount2(@Param("dto") FindAuditFinanceDto findAuditFinanceDto,
                                   @Param("userManageAreaId") String userManageAreaId,
                                   @Param("userAuditFinanceStatusStr") String userAuditFinanceStatusStr,
                                   @Param("lastMonthDay") String lastMonthDay ,
                                   @Param("userId") Integer userId);

    /**
     * 查看需要审核的列表
     *
     * @param findAuditFinanceDto
     * @param userManageAreaId
     * @param userAuditFinanceStatusStr
     * @param lastMonthDay
     * @return
     */
    @Select("<script>" +
            "SELECT af.* , du.avatar , du.username FROM axh_finance af LEFT JOIN dt_users du ON af.create_user_id = du.id " +
            "  WHERE af.is_valid = 1 AND af.current_status IN (${userAuditFinanceStatusStr}) " +
            " <if test='dto.reportType != null'>" +
            "     AND report_type = #{dto.reportType}  " +
            "</if> " +
            " <if test='userManageAreaId != null'>" +
            "     AND af.from_to_area_id IN (${userManageAreaId})  " +
            "</if> " +
            " <if test='dto.keyword != null'>" +
            "     AND af.finance_title LIKE '%${dto.keyword}%'  " +
            "</if> " +
            " <if test='dto.month != null'>" +
            "     AND af.create_time &gt;= '${dto.month}-01 00:00:00' AND af.create_time &lt;= '${lastMonthDay}' " +
            "</if> " +
            " <if test='dto.fromToAreaIds != null'>" +
            "     AND af.from_to_area_id IN (${dto.fromToAreaIds})  " +
            "</if> " +
            " <if test='dto.areaTypeStr != null'>" +
            "     AND af.area_type IN (${dto.areaTypeStr})  " +
            "</if> " +
            "</script>")
    List<FindAuditFinanceVo> findFinanceAudit(@Param("dto") FindAuditFinanceDto findAuditFinanceDto,
                                              @Param("userManageAreaId") String userManageAreaId,
                                              @Param("userAuditFinanceStatusStr") String userAuditFinanceStatusStr,
                                              @Param("lastMonthDay") String lastMonthDay);

    @Select("<script>" +
            " SELECT a1.* FROM (" +
            "  SELECT af.* , du.avatar , du.username FROM axh_finance af LEFT JOIN dt_users du ON af.create_user_id = du.id " +
            "  WHERE af.is_valid = 1 " +
            " <if test='dto.reportType != null'>" +
            "     AND report_type = #{dto.reportType}  " +
            "</if> " +
            " <if test='userManageAreaId != null'>" +
            "     AND af.from_to_area_id IN (${userManageAreaId})  " +
            "</if> " +
            " <if test='dto.keyword != null'>" +
            "     AND af.finance_title LIKE '%${dto.keyword}%'  " +
            "</if> " +
            " <if test='dto.month != null'>" +
            "     AND af.create_time &gt;= '${dto.month}-01 00:00:00' AND af.create_time &lt;= '${lastMonthDay}' " +
            "</if> " +
            " <if test='dto.fromToAreaIds != null'>" +
            "     AND af.from_to_area_id IN (${dto.fromToAreaIds})  " +
            "</if> " +
            " <if test='dto.areaTypeStr != null'>" +
            "     AND af.area_type IN (${dto.areaTypeStr})  " +
            "</if>" +
            " ) a1 LEFT JOIN axh_finance_log afl ON a1.id = afl.finance_id " +
            " WHERE afl.command_user_id = #{userId}  " +
            " GROUP BY a1.id " +
            "</script>")
    List<FindAuditFinanceVo> findFinanceAudit2(@Param("dto") FindAuditFinanceDto findAuditFinanceDto,
                                               @Param("userManageAreaId") String userManageAreaId,
                                               @Param("userAuditFinanceStatusStr") String userAuditFinanceStatusStr,
                                               @Param("lastMonthDay") String lastMonthDay ,
                                               @Param("userId") Integer userId);
}
